<template>
	<view class="content">
		<mescroll-uni ref="mescrollRef" @init="mescrollInit"  @up="upCallback" :up="upOption" :down="{use: false}">
			<view class="section-0">
				<view class="section-body">
					<view :class="`item ${curBoard === item.state ? 'item__on': ''}`" v-for="item in board" :key="item.name" @click="setCurBoard(item)">
						<view class="shape" v-if="item.right"></view>
						<view class="shape2" v-if="item.bottom"></view>
						<view class="top">{{item.name}}</view>
						<view class="bottom">{{item.count}}</view>
					</view>
				</view>
			</view>
			<view class="section-1">
			<view class="form">
				<view class="form-item">
					<view class="label">项目名称</view>
					<view class="shape"></view>
					<view class="input">
						<u--input
							placeholderClass="input-placeholder"
							class="input-body"
						    v-model="form.project_name"
						  ></u--input>
					</view>
				</view>
				<view class="form-item size6">
					<view class="label">停电起始时间</view>
					<view class="shape"></view>
					<view class="input">
						<view class="input-body" @click="showStart = true">{{form.start_time}}</view>
						<view class="iconfont icon-rili"></view>
					</view>
				</view>
				<view class="form-item size6">
					<view class="label">停电截止时间</view>
					<view class="shape"></view>
					<view class="input">
						<view class="input-body" @click="showEnd = true">{{form.end_time}}</view>
						<view class="iconfont icon-rili"></view>
					</view>
				</view>
				
				<formArea :value="form.city" :labelWidth="134" @onChange="(e) => form.city = e"></formArea>
				<!-- <view class="form-item size2">
					<view class="label">地区</view>
					<view class="shape"></view>
					<view class="input">
						<u--input
							placeholderClass="input-placeholder"
							class="input-body"
						    v-model="form.city"
						  ></u--input>
					</view>
				</view> -->
				<view class="actions">
					<view class="action" @click="handleResetForm()">
						<text>重</text>
						<text>置</text>
					</view>
					<view class="action search" @click="handleSearch()">
						<text>查</text>
						<text>询</text>
					</view>
				</view>
			</view>
			<view class="list">
				<view class="item" v-for="(item, index) in list" :key="item.id + index">
					<view class="parent">
						<view class="action" @click="expandChange(item.id)">
							<view class="iconfont icon-xiangyou1"></view>
						</view>
						<view class="name">{{index + 1}}.{{item.project_name}}</view>
					</view>
					
					<view class="child" v-if="expandList.indexOf(item.id) !== -1">
						<view class="child-item" @click="go(item2)" v-for="(item2, idx) in item.children" :key="item2.id + idx">
							<view class="top">
								<view class="name">
									{{index + 1}}-{{idx + 1}}.{{item2.project_name}}
								</view>
								<view class="desc">
									<text>起始时间 {{item2.start_date && item2.start_date.substring(0,10)}}</text>
									<text>截止时间 {{item2.end_date && item2.end_date.substring(0,10)}}</text>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		</mescroll-uni>
		<u-datetime-picker
			:show="showStart"
			v-model="startStamp"
			closeOnClickOverlay
			mode="datetime"
			@confirm="confirmStart"
			@cancel="showStart = false"
			@close="showStart = false"
		></u-datetime-picker>
		<u-datetime-picker
			:show="showEnd"
			v-model="startStamp"
			closeOnClickOverlay
			mode="datetime"
			@confirm="confirmEnd"
			@cancel="showEnd = false"
			@close="showEnd = false"
		></u-datetime-picker>
		
		<u-popup mode="center" :show="showQuestion" @close="closeQuestionPop" :round="50">
			<view class="pop project-pop">
				<view class="title">
					<text>履约评价</text>
				</view>
				<view class="body">
					<text class="date">{{date}}</text>
					<view class="wrapper _scroll">
						<view class="section-4">
							<view class="form-item upload-wrap">
								
								<view class="label">履约状态</view>
								<!-- <u-upload :fileList="fileList" :previewFullImage="true" @afterRead="afterRead"
									@delete="deletePic" multiple :maxCount="1"
									:style="{position:'relative',height:0== 0 ? '44rpx' : 'unset',border: 0== 0 ? 'solid 2rpx #1534ce' : 'none', borderRadius: '20rpx'}">
									<view class="iconfont icon-tupian"></view>
								</u-upload> -->
								
								<view class="radio-content">
									<view class="radio-content" @click="changeRadio1">
										<view class="radio" :class="radio1 == 1 ? 'radio-default':''">
											<view :class="radio1 == 1 ? 'radio-active':''"></view>
										</view>
										<text>正常</text>
									</view>
									<view class="radio-content radio-right" @click="changeRadio2">
										<view class="radio" :class="radio1 == 2 ? 'radio-default':''">
											<view :class="radio1 == 2 ? 'radio-active':''"></view>
										</view>
										<text>不良</text>
									</view>
								</view>
								
								
								
							</view>
							<view class="form-item textarea-wrap">
								<view class="label">履约评价</view>
								<view class="text-area">
									<u--textarea
										:maxlength="-1"
										placeholderClass="input-placeholder"
										class="text-body"
									    placeholder="请输入"
									    v-model="form.remark"
										border='null'
									  ></u--textarea>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="actions">
					<view class="custom-button cancel" @click="closeQuestionPop()">
						重置
					</view>
					<view class="custom-button" @click="submit">
						新增
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		addProject,
	} from '@/api/patrol.js';
	import { projectSignInAddApi, projectSignInIndexApi } from '@/api/duty.js';

	import formArea from '@/components/form_area/index.vue';
	import { powerListtApi, powerAnalysisApi } from '@/api/project.js';
	import { mapGetters, mapActions} from 'vuex';
	export default {
		components: {
			formArea
		},
		data() {
			return {
				radio1: 1,
				date: new Date().toISOString().slice(0, 10),
				showQuestion: true,
				mescroll: null, // mescroll实例对象 (此行可删,mixins已默认)
				upOption:{
					textNoMore: '-- 没有更多了 --'
				},
				form: {
					start_time: '',
					end_time: '',
					project_name: '',
					city: '',
				},
				pageSize:20,
				startStamp: 0,
				endStamp: 0,
				area: '',
				board: [
					{ name: '今日履约', count: 0, right: true, bottom: false, state: 1 },
					{ name: '本月履约', count: 0, right: true,  bottom: false, state: 2 },
					{ name: '下月履约', count: 0, bottom: true, state: 3 },
				],
				list: [],
				expandList: [],
				showStart: false,
				showEnd: false,
				total: {
					next_month: 0,
					td_month: 0,
					td_num: 0,
				},
				curBoard: 1
			};
		},
		methods: {
			
			changeRadio1() {
				this.radio1 = 1;
				console.log("正常")
			},
			changeRadio2() {
				this.radio1 = 2;
				console.log("不良")
			}, 
			setCurBoard(item) {
				this.curBoard = item.state;
				this.mescroll.resetUpScroll();
				this.mescroll.scrollTo(0,0);
			},
			async expandChange(id, project_id) {
				const idx = this.expandList.indexOf(id);
				if (idx !== -1) {
					this.expandList.splice(idx, 1);
				} else {
					this.expandList.push(id);
				}
				if (project_id) {
					if (!this.list[id.split('-')[0]].children[id.split('-')[1]].children) {
						this.fetchInfo(id, project_id)
					}
				}
			},
			
			go(path) {
				uni.navigateTo({
					url: path
				})
			},
			formatDate(timeStamp) {
				const date =new Date(timeStamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
				const Y = date.getFullYear() +'-';
			    const M = (date.getMonth() +1 <10 ?'0' + (date.getMonth() +1) : date.getMonth() +1) +'-';
			    const D = date.getDate();
			    const h = date.getHours() +':';
			    const m = date.getMinutes() +':';
			    const s = date.getSeconds();
			   	return Y + M + D + ' ' + h + m + s;  
			},
			closeQuestionPop() {
				this.showQuestion = false
			},
			confirmStart(val) {
				const timeStamp = val.value;
				this.form.start_time = this.formatDate(timeStamp);	
				this.showStart = false;
			},
			confirmEnd(val) {
				const timeStamp = val.value;
				this.form.end_time = this.formatDate(timeStamp);	
				this.showEnd = false;
			},
			/*mescroll组件初始化的回调,可获取到mescroll对象 (此处可删,mixins已默认)*/
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},
			/*上拉加载的回调*/
			async upCallback(page) {
				let obj = {
					...this.form,
					state: this.curBoard
				};
				if (this.form.start_time == '') {
					delete obj.start_time;
				}
				if (this.form.end_time == '') {
					delete obj.end_time;
				}
				const {data} =  await powerListtApi({
					...obj,
					type: 0,
					page: page.num,
					pageSize: this.pageSize
				});
				
				if (data.code === 1 || data.code === 200) {				
					// 接口返回的当前页数据列表 (数组)
					let arr = [];
					for (let key in data.data.data) {
						arr.push(data.data.data[key]);
					}
					let curPageData = arr; 
					// 接口返回的当前页数据长度 (如列表有26个数据,当前页返回8个,则curPageLen=8)
					let curPageLen = curPageData.length; 
					// 接口返回的总页数 (如列表有26个数据,每页10条,共3页; 则totalPage=3)
					let totalPage = data.data.data.last_page; 
					//设置列表数据
					if(page.num == 1) this.list = []; //如果是第一页需手动置空列表
					this.list = this.list.concat(curPageData); //追加新数据
					// 请求成功,隐藏加载状态
					this.mescroll.endByPage(curPageLen, totalPage); 
					setTimeout(()=>{
						this.mescroll.endSuccess(curPageLen);
					},20);
				} else{
					//  请求失败,隐藏加载状态
					this.mescroll.endErr()
				}
			},
			handleResetForm(){
				for (let key in this.form) {
					this.form[key] = '';
				}
				this.mescroll.resetUpScroll();
				this.mescroll.scrollTo(0,0);
			},
			handleSearch(){
				this.mescroll.resetUpScroll();
				this.mescroll.scrollTo(0,0);
			},
			async getTotal() {
				uni.showLoading({title: '加载中...'});
				const { data } = await powerAnalysisApi();
				uni.hideLoading();
				this.board[0].count = data.data.td_num;
				this.board[1].count = data.data.td_month;
				this.board[2].count = data.data.next_month;
			}
		},
		onReady() {
			this.startStamp = (new Date()).valueOf();
			this.endStamp = (new Date()).valueOf();
			this.getTotal();
		}
	}
</script>

<style lang="scss" scoped>
	
	.custom-empty{
		position: static;
	}
	.content{
		background-color: #fff;
	}
	.section-1{
		position: relative;
		height: 400rpx;
		top: -50rpx;
		z-index: 10;
		background-color: #fff;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;
		.list{
		
			
			width: 670rpx;
			box-sizing: border-box;
			margin-left: 40rpx;
			border-top: 2rpx solid #d8e4f2;
		
			.item {
				width: 100%;
				border-bottom: solid 2rpx #f2f2f0;
		
				.parent {
					width: 100%;
					@include clear();
					.name {					
						width: 550rpx;
						line-height: 30rpx;
						padding: 26rpx 0;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 2rpx;
						color: #3b3c40;
						float: left;
					}
		
					.count {
						float: right;
						min-width: 46rpx;
						height: 46rpx;
						margin-top: 18rpx;
						border-radius: 28rpx;
						line-height: 46rpx;
						text-align: center;
						font-size: 26rpx;
						font-weight: bold;
						color: #395cf5;
						background: #ffde00;
						text {
							font-size: 16rpx;
						}
					}
		
					.action {
						float: left;
						width: 32rpx;
						margin-right: 18rpx;
						line-height: 82rpx;
						text-align: center;
						color: #3b3c40;
		
						.iconfont {
							font-size: 32rpx;
							transition: 0.2s ease;
							transform: rotate(90deg);
						}
		
					}
				}
				.child {
					width: 750rpx;
					margin-left: - 40rpx;
					background: #f2f2fa;
		
					.child-item {
						
						width: 100%;
						border-bottom: 2rpx solid #e3e8f5;
						box-sizing: border-box;
						padding-left: 50rpx;
						@include clear();
						&:last-child {
							border-bottom: none;
						}
						
						
						.top {
							width: 100%;
							overflow: hidden;
							box-sizing: border-box;
							padding-left: 40rpx;
		
							.name {
								float: left;
								width: 100%;
								box-sizing: border-box;
								padding: 26rpx 0;
								font-size: 26rpx;
								color: #3b3c40;
								font-size: 26rpx;
							}
							.desc{
								float: left;
								width: 100%;
								font-size: 16rpx;
								color: #9b9b9b;
								box-sizing: border-box;
								padding-left: 50rpx;
								padding-bottom: 26rpx;
								text{
									margin-right: 30rpx;
									display: inline-block;
								}
							}
							.action {
								width: 32rpx;
								line-height: 82rpx;
								float: left;
								text-align: center;
								color: #7f98ae;
								margin-right: 18rpx;
								.iconfont {
									font-size: 32rpx;
									transition: 0.2s ease;
									transform: rotate(90deg);
								}
							}
						}
					}
				}
				
				.grad{
					width: 100%;
					background: #d9d9e2;
					.grad-item{
						width: 100%;
						border-bottom: 2rpx solid #e3e8f5;
						
						&:last-child {
							border-bottom: none;
						}
						
						.grad-top {
							width: 100%;
							overflow: hidden;
							box-sizing: border-box;
							padding-left: 40rpx;
							.name {
								float: left;
								width: calc(100% - 40rpx);
								padding: 26rpx 0;
								padding-bottom: 10rpx;
								font-size: 26rpx;
								color: #3b3c40;
								padding-left: 50rpx;
								box-sizing: border-box;
							}
							.desc{
								float: left;
								width: calc(100% - 40rpx);
								font-size: 16rpx;
								color: #9b9b9b;
								box-sizing: border-box;
								padding-left: 50rpx;
								padding-bottom: 26rpx;
								text{
									min-width: 300rpx;
									margin-right: 30rpx;
									display: inline-block;
								}
							}
						}
					}
				}
				.action__active{
					.iconfont{
						transform: rotate(-90deg)!important;
					}
				}
			}
		
			.item__expand {
					.parent {
						.action {
							.iconfont {
								transform: rotate(-90deg);
							}
						}
					}
			
					.child {
						box-shadow: 0rpx 10rpx 10rpx 0rpx rgba(0, 0, 0, 0.05) inset;
					}
				}
			
			
		}
		.actions{
			width: 100%;
			height: 50rpx;
			margin-top: 29rpx;
			padding-bottom: 40rpx;
			display: flex;
			justify-content: space-between;
			.action{
				width: 288rpx;
				height: 48rpx;
				background-color: #395cf5;
				border-radius: 20rpx;
				text-align: center;
				line-height: 48rpx;
				font-size: 26rpx;
				font-weight: normal;
				font-stretch: normal;
				line-height: 48rpx;
				color: #ffffff;
				text{
					&:first-child{
						margin-right: 20rpx;
					}
				}
			}
			.search{
				background-color: #1534ce;
			}
		}
		.form{
			width: 100%;
			padding: 0 75rpx;
			padding-top: 40rpx;
			box-sizing: border-box;
			.form-item{
				width: 600rpx;
				height: 48rpx;
				border-radius: 20rpx;
				border: solid 2rpx #1534ce;
				margin: 0 auto;
				margin-bottom: 25rpx;
				&:last-child{
					margin-bottom: 34rpx;
				}
				@include clear();
				.label{
					float: left;
					width: 134rpx;
					height: 48rpx;
					text-align: center;
					line-height: 44rpx;
					font-size: 26rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #1534ce;
				}
				.shape{
					width: 3rpx;
					height: 32rpx;
					background-color: #7f98ae;
					float: left;
					margin-top: 8rpx;
				}
				.input{
					float: left;
					width: calc(100% - 134rpx - 3rpx);
					.input-body{
						box-sizing: border-box;
						width: 100%;
						height: 44rpx;
						line-height: 44rpx;
						padding: 0!important;
						border: none;
						padding-left: 30rpx!important;
						padding-right: 30rpx!important;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0,0,0,0.7);
					}
					.input-placeholder{
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0,0,0,0.5);
					}
				}	
			}
			.size6{
				.label{
					width: 184rpx;
				}
				.input{
					width: calc(100% - 184rpx - 3rpx);
					position: relative;
					.iconfont{
						position: absolute;
						right: 25rpx;
						top: 7rpx;
						font-size: 30rpx;
						color: #dad9df;
					}
				}
			}
			.size2{
				.label{
					width: 77rpx;
				}
				.input{
					width: calc(100% - 77rpx - 3rpx);
				}
			}
		}
	}
	.section-0{
		width: 100%;
		background-color: #2e5efe;
		.section-body{
			width: 100%;
			padding-left: 40rpx;
			padding-right: 40rpx;
			padding-bottom: 50rpx;
			padding-top: 20rpx;
			background-color: #1534ce;
			background-size: cover;
			background-repeat: no-repeat;
			border-top-left-radius: 50rpx;
			border-top-right-radius: 50rpx;
			overflow: hidden;
			box-sizing: border-box;
			.item{
				width: 33.33%;
				float: left;
				display: flex;
				flex-wrap: wrap;
				align-content: space-between;
				height: 100rpx;
				position: relative;
				padding-top: 20rpx;
				padding-bottom: 20rpx;
				.shape{
					width: 4rpx;
					height: 84rpx;
					background-image: linear-gradient(rgba(#3658f2, 0.3) 0%, #3658f2 30%, #3658f2 70%, rgba(#3658f2, 0.3) 100%,);
					position: absolute;
					right: 0;
					top: 28rpx;
				}
				.shape2{
					height: 4rpx;
					width: 172rpx;
					background-image: linear-gradient(90deg, rgba(#3658f2, 0.3) 0%, #3658f2 30%, #3658f2 70%, rgba(#3658f2, 0.3) 100%,);
					position: absolute;					
					left: 0;
					right: 0;
					margin: 0 auto;
					bottom: -2rpx;
				}
				.top{
					width: 100%;
					text-align: center;
					font-size: 16rpx;
					font-weight: normal;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #95c4fa;
					transition: 0.3s ease;
				}
				.bottom{
					width: 100%;
					text-align: center;
					font-size: 58rpx;
					font-weight: bold;
					font-stretch: normal;
					letter-spacing: 0rpx;
					color: #ffde00;
					transition: 0.3s ease;
				}
			}
			.item__on{
				.top{
					text-shadow: 8rpx 8rpx 8rpx rgba(0, 0, 0, 0.3);
				}
				.bottom{
					text-shadow: 8rpx 8rpx 8rpx rgba(0, 0, 0, 0.3);
				}
			}
		}
	}
	.pop {
		margin-top: -150rpx;
		height: 625rpx;
		width: 671rpx;
		padding-bottom: 80rpx;
		background-color: #fff;
		-webkit-border-radius: 35rpx;
		-moz-border-radius: 35rpx;
		border-radius: 35rpx;
		overflow: hidden;
		.date{
			margin-top: 30rpx;
			margin-right: 40rpx;
			margin-bottom: 30rpx;
		    text-align: right;
			display: block;
			// width: 98rpx;
			// height: 15rpx;
			font-family: SourceHanSansCN-Regular;
			font-size: 19rpx;
			font-weight: normal;
			// font-stretch: normal;
			// line-height: 34rpx;
			// letter-spacing: 0rpx;
			color: #8497ac;
		    }
		.actions{
			margin-top: -130rpx;
			width: 100%;
			display: flex;
			justify-content: space-between;
			.cancel{
				background-color: #395cf5;
			}
		}
		/deep/ .mescroll-uni-fixed {
			position: inherit;
		}
		
		.title {
			width: 100%;
			height: 96rpx;
			line-height: 96rpx;
			background-color: #1534ce;
			font-size: 42rpx;
			font-weight: normal;
			font-stretch: normal;
			letter-spacing: 4rpx;
			color: #ffffff;
			text-align: center;
		}
		
		.form-item{
				width: 600rpx;
				height: 48rpx;
				line-height: 48rpx;
				margin: 0 auto;
				margin-bottom: 25rpx;
				display: flex;
				align-items: center;
			
				.radio-content {
					  height: 40rpx;
					  display: flex;
					  align-items: center;
					}
					.radio {
					  width: 24rpx;
					  height: 24rpx;
					  border-radius: 50%;
					  border: 2rpx solid #1534ce;
					  display: flex;
					  flex-direction: column; 
					  align-items: center;
					  justify-content: center;
					  margin: 0rpx 26rpx 0rpx 15rpx;
					}
					.radio-active{
					  width: 16rpx; 
					  height: 16rpx;
					  border-radius: 50%;
					  background-color: #1534ce;
					}
					.radio-default{
					  border: 2rpx solid #1534ce;
					}
					.radio-right {
					  margin-left: 66rpx;
					}
				
				&:last-child {
					margin-bottom: 34rpx;
				}
			
				&.upload-wrap {
					align-items: unset;
					height: unset;
					line-height: unset;
				}
			
				@include clear();
			
				.label {
					float: left;
					width: 130rpx;
					height: 48rpx;
					text-align: left;
					line-height: 44rpx;
					font-size: 28rpx;
					color: #46464a;
				}
			
				/deep/ .u-upload__wrap {
					width: calc(100% - 206rpx) !important;
					background-color: transparent;
				}
			
				.icon-tupian {
					position: absolute;
					right: 25rpx;
					line-height: 44rpx;
					color: #1534ce;
					font-size: 28rpx;
				}
			
				/deep/ .u-radio-label--left {
					margin-right: 55rpx;
				}
			
				/deep/ .u-radio__text {
					font-size: 26rpx !important;
					color: #46464a !important;
				}
			
				.text-area {
					float: left;
					width: calc(100% - 134rpx - 3rpx);
					height: 270rpx;
					border-radius: 20rpx;
					border: solid 2rpx #1534ce;
			
					.input-body {
						box-sizing: border-box;
						width: 100%;
						height: 44rpx;
						padding: 0 !important;
						border: none;
						padding-left: 30rpx !important;
						padding-right: 30rpx !important;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.7);
					}
			
					.text-body {
						box-sizing: border-box;
						width: 100%;
						height: 400rpx;
						border: none;
						padding: 10rpx 25rpx!important;
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.7);
						background: none;
					}
						
					.input-placeholder {
						font-size: 26rpx;
						font-weight: normal;
						font-stretch: normal;
						letter-spacing: 0rpx;
						color: rgba(0, 0, 0, 0.5);
					}
				}
			
		}
		.textarea-wrap{
			height: 400rpx;
			display: block;
		}
		
	}
</style>
